package com.zhhs.project.act.activity.controller;



import com.zhhs.common.utils.poi.ExcelUtil;
import com.zhhs.framework.aspectj.lang.annotation.Log;
import com.zhhs.framework.aspectj.lang.enums.BusinessType;
import com.zhhs.framework.web.controller.BaseController;
import com.zhhs.framework.web.domain.AjaxResult;
import com.zhhs.framework.web.page.TableDataInfo;
import com.zhhs.project.act.activity.domian.ActActivityAdvanced;
import com.zhhs.project.act.activity.domian.ActActivityAwardWay;
import com.zhhs.project.act.activity.domian.ActActivityBasics;
import com.zhhs.project.act.activity.domian.ActActivityPrize;
import com.zhhs.project.act.activity.service.IActActivityAdvancedService;
import com.zhhs.project.act.activity.service.IActActivityAwardWayService;
import com.zhhs.project.act.activity.service.IActActivityBasicsService;
import com.zhhs.project.act.activity.service.IActActivityPrizeService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 基础设置Controller
 *
 * @author zhhs
 * @date 2022-11-09
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wecom/activity/basics")
public class ActActivityBasicsController extends BaseController
{

    @Autowired
    private IActActivityBasicsService actActivityBasicsService;
    @Autowired
    private IActActivityAwardWayService actActivityAwardWayService;
    @Autowired
    private IActActivityPrizeService actActivityPrizeService;

    @Autowired
    private IActActivityAdvancedService actActivityAdvancedService;
    /**
     * 查询基础设置列表
     */
    @PreAuthorize("@ss.hasPermi('zhhs:activitybasics:list')")
    @GetMapping("/list")
    public TableDataInfo list(ActActivityBasics actActivityBasics)
    {
        startPage();
        List<ActActivityBasics> list = actActivityBasicsService.selectActActivityBasicsList(actActivityBasics);
        return getDataTable(list);
    }

    /**
     * 导出基础设置列表
     */
    @PreAuthorize(value = "@ss.hasPermi('zhhs:activitybasics:export')")
    @Log(title = "基础设置" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(ActActivityBasics actActivityBasics)
    {
        List<ActActivityBasics> list = actActivityBasicsService.selectActActivityBasicsList(actActivityBasics);
        ExcelUtil<ActActivityBasics> util = new ExcelUtil<ActActivityBasics>(ActActivityBasics.class);
        return util.exportExcel(list, "基础设置数据");
    }


    /**
     * 新增基础设置
     */
    @PreAuthorize("@ss.hasPermi('zhhs:activitybasics:add')" )
    @Log(title = "基础设置" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addSave(ActActivityBasics actActivityBasics)
    {
        return toAjax(actActivityBasicsService.insertActActivityBasics(actActivityBasics));
    }

    @GetMapping("/getEditDate" )
    public AjaxResult edit(Long activityId)
    {

        Map<String, Object> map = new HashMap<>();
        ActActivityBasics actActivityBasics = new ActActivityBasics();
        actActivityBasics.setActivityId(activityId);
        List<ActActivityBasics> activityBasicList = actActivityBasicsService.selectActActivityBasicsList(actActivityBasics);
        if (CollectionUtils.isNotEmpty(activityBasicList)){
            map.put("actActivityBasics", activityBasicList.get(0));
        }

        ActActivityAwardWay actActivityAwardWay = new ActActivityAwardWay();
        actActivityAwardWay.setActivityId(activityId);
        List<ActActivityAwardWay> activityAwardWayList = actActivityAwardWayService.selectActActivityAwardWayList(actActivityAwardWay);
        if (CollectionUtils.isNotEmpty(activityAwardWayList)){
            map.put("actActivityAwardWay", activityAwardWayList.get(0));
        }

        ActActivityPrize actActivityPrize = new ActActivityPrize();
        actActivityPrize.setActivityId(activityId);
        List<ActActivityPrize> activityPrizeList = actActivityPrizeService.selectActActivityPrizeList(actActivityPrize);
        map.put("actActivityPrize", activityPrizeList);


        ActActivityAdvanced actActivityAdvanced = new ActActivityAdvanced();
        actActivityAdvanced.setActivityId(activityId);
        List<ActActivityAdvanced> activityAdvancedList = actActivityAdvancedService.selectActActivityAdvancedList(actActivityAdvanced);
        if (CollectionUtils.isNotEmpty(activityAdvancedList)){
            map.put("actActivityAdvanced", activityAdvancedList.get(0));
        }
        return AjaxResult.success(map);
    }

    /**
     * 修改基础设置
     */
    @PreAuthorize("@ss.hasPermi('zhhs:activitybasics:edit')" )
    @Log(title = "基础设置" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult editSave(ActActivityBasics actActivityBasics)
    {
        return toAjax(actActivityBasicsService.updateActActivityBasics(actActivityBasics));
    }

    /**
     * 删除基础设置
     */
    @PreAuthorize("@ss.hasPermi('zhhs:activitybasics:remove')" )
    @Log(title = "基础设置" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(actActivityBasicsService.deleteActActivityBasicsByIds(String.valueOf(ids)));
    }
}
